<?php

/**
 * Classe Category: gestisce le categorie del forum, ogni categoria può avere molti forum e un forum una sola categoria.
 *
 * @author Stefano Poncini
 */

include_once("Forum.class.php");

class Category {
    public $name;
    public $desc;
    public $creation_date;
    public $creator;
    public $visible;
    public $forums = array();
    
    /**
     * Costruttore della classe Category, crea un istanza e valorizza le variabili di classe
     * 
     * @param string $name
     * @param string $desc
     * @param date $creation_date
     * @param string $creator
     * @param int $visible
     */
    public function __construct($name, $desc, $creation_date, $creator, $visible){
        
        $this->name = $name;
        $this->desc = $desc;
        $this->creation_date = $creation_date;
        $this->create = $creator;
        $this->visible = $visible;
    }
    
    /**
     * Metodo getForums: ritorna tutti i forum associati alla categoria
     * 
     * @return \Forum
     */
    public function getForums(){
        $sql = "SELECT * FROM forum where category = ? and visible = 1 ";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute(array($this->name));

        while ($forum = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $this->forums[] = new Forum($forum['id_forum'], $forum['name'], $forum['desc'], $forum['creation_date'], $forum['creator'], $forum['status'], $forum['type'], $forum['category'], $forum['visible']);
        }

        return $this->forums;
    }
    
    /**
     * Metodo getCategories: ritorna tutte le categorie
     * 
     * @static
     * @return \Category
     */
    public static function getCategories(){
        $categories = array();
        $sql = "SELECT * FROM category where visible = 1";
        $stmt = DB::getDBO()->prepare($sql);
        $stmt->execute();

        while ($cat = $stmt->fetch(PDO::FETCH_ASSOC)) {
            $categories[] = new Category($cat['name'], $cat['desc'], $cat['creation_date'], $cat['creator'], $cat['visible']);
        }

        return $categories;
        
    }
    
    /**
     * Metodo newCategory: crea una nuova categoria
     * 
     * @param string $name
     * @param string $desc
     * @param string $user
     * @return boolean
     */
    public static function newCategory($name, $desc, $user){
        try {
            $sql = "INSERT INTO category(`name`,`desc`,`creator`,`creation_date`,`visible`) VALUES(?,?,?,?,?)";

            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array($name, $desc, $user, date("Y-m-d H:i:s", time()), 1));

            return true;
        } catch (PDOException $e) {

            return false;
        }
    }
    
    public static function newForum($name, $desc, $cat, $user){
        try {
            $sql = "INSERT INTO forum(`id_forum`,`name`,`desc`,`status`,`type`,`creator`,`creation_date`,`category`,`visible`) VALUES(?,?,?,?,?,?,?,?,?)";

            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(null, $name, $desc, 1,1,$user, date("Y-m-d H:i:s", time()), $cat, 1));

            return true;
        } catch (PDOException $e) {

            return false;
        }
    }
    
    public static function removeCategory($id){
        try {
            $sql = "UPDATE category SET visible = ? WHERE name = ?";
            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(0,$id));
            return true;
        } catch (PDOException $e) {

            return false;
        }
        
    }
}

?>
